var app = getApp();
// import testBackgroundData from "../../../pages/utils/testBackgroundData"
// import testMenuData from "../../../pages/utils/testMenuData.js"

Component({
    properties: {
        textOne: {
            type: String
        },
        propShowBodyLays: {
            type: Array
        }
    },

    data: {
        baseUrl: app.globalData.baseUrl,
        showBackGroundImage: "/backgrounds/BG1-whole.jpg",
        showBackGroundFrame: "/backgrounds/bg1_css_sprites_v2.png",
        showBackGroundGif: "/backgrounds/bg1_frame.gif",

        randomStartTime: 0,
        randomEndTime: 0,
        randomDuring: 3000,
        randomBackgroundIndex: 0,

        hatTipStrLen: 0,
        hatTip: "hatTip",
        jacketTipStrLen: 0,
        jacketTip: "hatTip",
        pantsTipStrLen: 0,
        pantsTip: "hatTip",
        shoesTipStrLen: 0,
        shoesTip: "hatTip",
        packageTipStrLen: 0,
        packageTip: "hatTip",
        showTipLay: false,
        gPartsMenuDatas: {},
    },

    attached() {

    },

    methods: {
        doFrameAni() {
            this.setData({
                gPartsMenuDatas: app.globalData.gPartsMenuDatas
            })

            let timestamp = new Date().getTime();
            let tempPartList = this.data.propShowBodyLays;
            if (timestamp < this.data.randomEndTime) {
                // tempPartList.forEach((element,index) => {
                //     let tempPartSrcData = this.getRandomPartInfo(element);
                // });

                // debugger
                for (let i = 0; i < tempPartList.length; i++) {
                    let tempItem = tempPartList[i];



                    if (tempItem.partId == "hair")
                        continue;

                    if (tempItem.partId == "background") {
                        let tempPartSrcData = this.getRandomPartInfo(tempItem);

                        tempPartList[i].partSrcId = tempPartSrcData.dataId;
                        tempPartList[i].imageUrl = tempPartSrcData.imageUrl;
                        tempPartList[i].frameUrl = tempPartSrcData.frameUrl;
                        tempPartList[i].gifUrl = tempPartSrcData.gifUrl;

                        tempPartList[i].scenebackGifImage = tempPartSrcData.scenebackGifImage;
                        tempPartList[i].scenebackGifUrl = tempPartSrcData.scenebackGifUrl;
                        tempPartList[i].sceneBackBottomUrl = tempPartSrcData.sceneBackBottomUrl;

                        this.setData({
                            showBackGroundImage: tempPartSrcData.imageUrl,
                            showBackGroundFrame: '',
                            showBackGroundGif: '',
                            showTipLay: false
                        })
                        continue;
                    }



                    // if(tempItem.partId == "pants")
                    //     debugger

                    let tempPartSrcData = this.getRandomPartInfo(tempItem);
                    tempPartList[i].partSrcId = tempPartSrcData.dataId;
                    tempPartList[i].partImage = tempPartSrcData.srcUrl;
                    tempPartList[i].partDesc = tempPartSrcData.srcUrl;
                    tempPartList[i].partSourcePath = tempPartSrcData.sourcePath;



                    if (tempItem.partId != "bangs")
                        continue;

                    // let tempHairId = tempPartSrcData.hairId;
                    let tempHairId = tempPartSrcData.detailDataId;
                    let tempHairData = this.getHairPartInfoById(tempHairId);

                    let hairPart = tempPartList.find(e => {
                        return e.partId == "hair"
                    })

                    hairPart.partSrcId = tempHairData ? tempHairData.dataId : '';
                    hairPart.partImage = tempHairData ? tempHairData.srcUrl : '';
                }

                this.setData({
                    propShowBodyLays: tempPartList
                })

                setTimeout(() => {
                    this.doFrameAni();
                }, 200);
            } else {
                // // 背景动画结束
                // let tempIndex = this.data.randomBackgroundIndex;
                // // let tempIndex = 3;
                // this.setData({
                //     randomBackgroundIndex: tempIndex,
                //     showBackGroundImage: testBackgroundData[tempIndex].imageUrl,
                //     showBackGroundFrame: testBackgroundData[tempIndex].frameUrl,
                //     showBackGroundGif: testBackgroundData[tempIndex].gifUrl,
                //     showTipLay: true
                // })
                let backgroundPart = tempPartList.find(e => {
                    return e.partId == "background";
                })

                this.setData({
                    showBackGroundImage: backgroundPart.imageUrl,
                    showBackGroundFrame: backgroundPart.frameUrl,
                    showBackGroundGif: backgroundPart.gifUrl,
                    showTipLay: true
                })

                let _self = this;
                this.data.propShowBodyLays.forEach(element => {
                    // 提包
                    // debugger
                    let tempStr = element.partDesc ? element.partDesc : '';
                    if (element.partId == "package") {
                        _self.setData({
                            packageTip: tempStr,
                        })
                    }
                    // 裤子
                    else if (element.partId == "pants") {
                        _self.setData({
                            pantsTip: tempStr,
                        })
                    }
                    // 上衣
                    else if (element.partId == "jacket") {
                        _self.setData({
                            jacketTip: tempStr,
                        })
                    }
                    // 鞋子
                    else if (element.partId == "shoes") {
                        _self.setData({
                            shoesTip: tempStr,
                        })
                    }
                    // 帽子
                    else if (element.partId == "hat") {
                        _self.setData({
                            hatTip: tempStr,
                        })
                    }
                });

                this.triggerEvent("onRandomAniFinish",
                    {
                        showBodyLays: _self.data.propShowBodyLays,
                    }
                );
            }
        },

        startRandomAni(inDuring) {
            let timestamp = new Date().getTime();
            let tempRandomEnd = timestamp + inDuring;
            this.setData({
                randomEndTime: tempRandomEnd
            })

            this.doFrameAni();
        },

        getRandomPartInfo(inData) {
            let temp1 = this.randomNum(0, 0);
            let temp2 = this.randomNum(0, 1);



            let tempGender = app.globalData.selectGender;
            let tempPartSrcId = inData.partSrcId;

            let candidateOrignList = this.data.gPartsMenuDatas[inData.partId];
            let candidateList = [];
            candidateOrignList.forEach(e => {
                if (e.dataId != inData.partSrcId && (e.sexy == tempGender || e.sexy == 3))
                    candidateList.push(e);
            })

            if (candidateList.length == 0)
                candidateList = candidateOrignList;

            let randomIndex = 0;
            if (candidateList.length > 0)
                randomIndex = this.randomNum(0, candidateList.length - 1);

            let temp = candidateList[randomIndex];
            return temp;
        },

        //生成从minNum到maxNum的随机数
        randomNum(minNum, maxNum) {
            switch (arguments.length) {
                case 1:
                    return parseInt(Math.random() * minNum + 1, 10);
                    break;
                case 2:
                    return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
                    break;
                default:
                    return 0;
                    break;
            }
        },

        getHairPartInfoById(inHairId) {
            let candidateList = this.data.gPartsMenuDatas["hair"];
            return candidateList.find(e => {
                return e.dataId == inHairId;
            })
        },

        onTipClick(inData) {
            // debugger
            console.log(inData.currentTarget.dataset.type)
            let tempdata = this.data.propShowBodyLays.find(e => {
                return e.partId == inData.currentTarget.dataset.type
            })
            console.log(tempdata)

            this.triggerEvent("onPartTipClick", tempdata.dataId);
        }
    },
})